package fda.base;

import java.util.ArrayList;
import java.util.List;

public class Block {
	
	int runID;		// index of the sub. list after phase 1
	int blockID;	// index of the block in the sub. list after phase 1
	private List<Tuple> records;
	private final int MAX_TUPLES;
	
	public Block(int maxBlockSize) {
		this.MAX_TUPLES = maxBlockSize;
		records = new ArrayList<Tuple>();
	}
	
	public Block(List<Tuple> rec) {
		runID = -1;
		blockID = -1;
		records = rec;
		if(rec != null) {
			MAX_TUPLES = records.size();
		}
		else {
			MAX_TUPLES = 0;
		}
	}
	
	public Block(int r_id, int b_id, List<Tuple> rec) {
		runID = r_id;
		blockID = b_id;
		records = rec;
		MAX_TUPLES = records.size();
	}
	
	public Tuple getRecord(int index) {
		return records.get(index);
	}
	
	/**
	 * @return maximum number of tuples in the block
	 */
	public int getBlockSize(){
		return MAX_TUPLES;
	}
	
	/**
	 * @return list of Tuples
	 */
	public List<Tuple> getRecords() {
		return records;
	}
	
	/**
	 * @return TRUE if the Block is full and there is no space in Block to next record
	 */
	public boolean isFull(){
		return records.size() >= MAX_TUPLES;
	}
	
	/**
	 * Add record to next available place in the block. Throws error if record added to full block
	 * @param t record to add
	 */
	public void addRecord(Tuple t) {
		if(isFull())
			throw new IllegalStateException("Block is full, cannot add more tuples");
		
		records.add(t);
	}
	
	/**
	 * remove all tuples from block, and reset index
	 */
	public void clear(){
		records.clear();
	}
	
	public int getRunID() {
		return runID;
	}
	
	public int getBlockID() {
		return blockID;
	}
}
